﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartMotion.Core
{
   public class PagesMgr
    {
        #region 属性单例模式
        private static readonly Lazy<PagesMgr> Instance = new Lazy<PagesMgr>(() => new PagesMgr());
        private PagesMgr()
        {
            _panels = new Dictionary<string, UIPanel>();
            _pages = new Dictionary<string, Dictionary<string, UIPage>>();
        }
        public static PagesMgr Ins { get; } = Instance.Value;
        #endregion

        private readonly Dictionary<string, UIPanel> _panels;

        private readonly Dictionary<string, Dictionary<string, UIPage>> _pages;
        public bool RegisterPanel(UIPanel panel,string panelName="default")
        {
            if (panelName == "default") panelName = panel.Name;
            if (_panels.ContainsKey(panelName)) return false;
            _panels.Add(panelName, panel);
            return true;
        }
        
        public bool RegisterPageToPanel(string panelName,UIPage page,string pageName="default")
        {
            if (pageName == "default") pageName = page.GetType().Name;
            if (!_panels.ContainsKey(panelName)) return false;
            if (!_pages.ContainsKey(panelName)) 
                _pages.Add(panelName, new Dictionary<string, UIPage>());
            _pages[panelName].Add(pageName, page);
            return true;
        }
    
        public void ShowPage(string panelName,string pageName)
        {
            if (!_panels.ContainsKey(panelName)) return;
            if (!_pages.ContainsKey(panelName)) return;
            var panel = _panels[panelName];
            var page = _pages[panelName][pageName];
            panel.Controls.Clear();
            panel.Controls.Add(page);
            page.Show();
        }
    }
}
